package sy.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sy.dao.ProductDaoI;
import sy.dao.TransportDaoI;
import sy.dao.TransportDetailDaoI;
import sy.model.Tproduct;
import sy.model.Ttransport;
import sy.model.TtransportDetail;
import sy.pageModel.DataGrid;
import sy.pageModel.PageHelper;
import sy.pageModel.Product;
import sy.pageModel.TransportDetail;
import sy.service.TransportDetailServiceI;

import java.util.*;


@Service
public class TransportDetailServiceImpl implements TransportDetailServiceI {

	@Autowired
	private TransportDetailDaoI transportDetailDao;
	@Autowired
	private TransportDaoI transportDao;
	@Autowired
	private ProductDaoI productDao;


	private String whereHql(String id, Map<String, Object> params) {
		String hql = "";
		if (id != null) {
			hql += " where t.retransportId=" +id;
			

		}
		return hql;
	}

	private String orderHql(PageHelper ph) {
		String orderString = "";
		if (ph.getSort() != null && ph.getOrder() != null) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}

	@Override
	synchronized public void add(TransportDetail transportDetail) throws Exception {}
	

	@Override
	public TransportDetail get(String id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		TtransportDetail t = transportDetailDao.get("select distinct t from TretransportDetail t where t.id = :id", params);
		TransportDetail transportDetail = new TransportDetail();
		BeanUtils.copyProperties(t,transportDetail);
		return transportDetail;
	}

	@Override
	synchronized public void edit(TransportDetail transportDetail) throws Exception {}



	@Override
	public void delete(String id) {}


	
	@Override
	public DataGrid productCombogrid(String q, PageHelper ph) {
		if (q == null) {
			q = "";
		}
		DataGrid dg = new DataGrid();
		List<Product> ul = new ArrayList<Product>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("code", "%%" + q.trim() + "%%");
		List<Tproduct> tl = productDao.find("from Tproduct t where t.code like :code order by " + ph.getSort() + " " + ph.getOrder(), params, ph.getPage(), ph.getRows());
		if (tl != null && tl.size() > 0) {
			for (Tproduct t : tl) {
				Product p = new Product();
				p.setName(t.getName());
				p.setCode(t.getCode());
				ul.add(p);
			}
		}
		dg.setRows(ul);
		dg.setTotal(productDao.count("select count(*) from Tproduct t where t.code like :code", params));
		return dg;
	}
	
	
	@Override
	public DataGrid transportDetail(String id) {
		DataGrid dg = new DataGrid();
		List<TransportDetail> ul = new ArrayList<TransportDetail>();
		String hql = " from TtransportDetail t where t.transport.id ='" +id+"'";
		List<TtransportDetail> l = transportDetailDao.find(hql);
		if (l != null && l.size() > 0) {
			for (TtransportDetail t : l) {
				TransportDetail u = new TransportDetail();
				if(t.getTransport()!=null){
					Ttransport transport=t.getTransport();
					u.setTransportId(transport.getId());
					u.setTransportCode(transport.getCode());
				}
				if(t.getProduct()!=null){
					Tproduct product=t.getProduct();
					u.setProductId(product.getId());
					u.setProductCode(product.getCode());
					u.setProductName(product.getName());
					u.setProductDetail(product.getDetail());
				}
				BeanUtils.copyProperties(t, u);
				ul.add(u);
			}
		}
		dg.setRows(ul);
		dg.setTotal(0L);
		return dg;
	
	}
}
